package com.seemygo.shop.cloud.mq.listener;

import com.seemygo.shop.cloud.mq.msg.DelayOrderMsg;
import com.seemygo.shop.cloud.mq.msg.MQConstants;
import com.seemygo.shop.cloud.service.IOrderInfoService;
import com.seemygo.shop.cloud.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(
        topic = MQConstants.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstants.DELAY_ORDE_TAG,
        messageModel = MessageModel.BROADCASTING,
        consumerGroup = MQConstants.DELAY_ORDE_CONSUMER_GROUP
)
public class DelayOrderMQListener implements RocketMQListener<DelayOrderMsg> {

    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Override
    public void onMessage(DelayOrderMsg delayOrderMsg) {
        log.info("[延迟订单消息] 收到延迟订单消息，检查订单是否已经支付：{}", JSONUtil.toJSONString(delayOrderMsg));
        //检查订单是否超时超时进行库存回补
        iOrderInfoService.checkTimeout(delayOrderMsg.getOrderNo(), delayOrderMsg.getSeckillId());

    }
}
